import random

from requests_html import HTMLSession
# from curl_cffi import requests
import requests
import json
from bs4 import BeautifulSoup
from utils.wjx.common_obj import ItemWjxOb, DataIndex
from enums.sys_type_enum import SysTypeEnum

sessions = requests.Session()  # 统一在外部初始化session


def explain_www_wjx_cn(url: str):
    def convert_wjx_type_to_sys_type(t: str):
        """
        将问卷星的类型转换为自己的类型
        :param t:
        :return:
        """
        q_type_name = t
        if t == '1':
            q_type_name = SysTypeEnum.TEXT.value
        elif t == '2':
            pass
        elif t == '3':
            q_type_name = SysTypeEnum.SINGLE.value
        elif t == '4':
            q_type_name = SysTypeEnum.MULTIPLE.value
        elif t == '5':
            q_type_name = SysTypeEnum.SCALE.value
        elif t == '6':
            q_type_name = SysTypeEnum.MATRIX.value
        elif t == '7':
            q_type_name = SysTypeEnum.SINGLE_SELECT.value
        elif t == '8':
            q_type_name = SysTypeEnum.SINGLE_SLIDE.value
        elif t == '9':
            q_type_name = SysTypeEnum.SLIDE.value
        elif t == '10':
            pass
        elif t == '11':
            q_type_name = SysTypeEnum.SORTED.value
        elif t == '12':
            q_type_name = SysTypeEnum.RATE.value
        return q_type_name

    # session = HTMLSession()
    # response = session.get(url)
    # sessions = requests.Session()
    header = {
        'X-Forwarded-For': f'{112}.{random.randint(64, 68)}.{random.randint(0, 255)}.{random.randint(0, 255)}',
        # 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4'' (KHTML, like Gecko) Mobile/12A365 MicroMessenger/6.0 NetType/WIFI'
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
    }
    # response = sessions.get(url, impersonate="chrome101", allow_redirects=False, headers=header, timeout=10)
    response = requests.get(url, allow_redirects=False, headers=header, timeout=10)
    # 使用 BeautifulSoup 解析页面内容
    soup = BeautifulSoup(response.content, 'html.parser')
    # 将选择器转换为合适的 CSS 选择器
    # selector = 'fieldset'
    # # 定位到指定位置的元素
    # element = soup.select_one(selector)
    div_question = soup.find('div', {'id': 'divQuestion'})
    el = div_question.select('.field.ui-field-contain')
    res_list = []
    data_index = []
    cur_index = 0
    counter = 1
    for i, question in enumerate(el):
        item_data_index = []
        children = []
        options = []
        label = question.select_one('.field-label')
        has_jump = question.attrs.get('hasjump')
        any_jump = question.attrs.get('anyjump')
        relation = question.attrs.get('relation')  # 相关某些题
        title_id = question.attrs.get('topic')  # 题目的标题
        title = str(title_id) + label.text
        cur_type = question.attrs.get('type')  # 题目的类型
        minvalue = question.attrs.get('minvalue')  # 最大的选项个数
        maxvalue = question.attrs.get('maxvalue')  # 最大的选项个数
        cur_req = question.attrs.get('req')
        cur_min_oper_num = int(minvalue) if minvalue else None  # 最小值
        cur_max_oper_num = int(maxvalue) if maxvalue else None  # 最大值
        cur_real_type = cur_type
        sys_type = convert_wjx_type_to_sys_type(cur_real_type)  # 这个系统的题目类型 - 问卷星类型和系统类型的映射关系
        title_is_text = False  # 题目是否是填空题
        if cur_type == '1':
            title_is_text = True
            cur_input = question.select_one('input')
            verify = cur_input.attrs.get('verify')
            datebox = cur_input.attrs.get('data-role')
            if verify:
                cur_real_type = SysTypeEnum.AREA.value
            elif datebox == 'datebox':
                cur_real_type = SysTypeEnum.DATE.value
            else:
                cur_real_type = SysTypeEnum.TEXT.value
            option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=title, title_type=cur_type,
                                   sys_type=convert_wjx_type_to_sys_type(cur_real_type), is_jump=False, req=cur_req,
                                   is_text=True, relation=relation, data_index=cur_index)
            options.append(option_obj.__dict__)
            di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=title, title_type=cur_type,
                           sys_type=convert_wjx_type_to_sys_type(cur_real_type), req=cur_req, is_text=True,
                           relation=relation, options=options)
            data_index.append(di.__dict__)
            item_data_index.append(cur_index)
            cur_index += 1
        elif cur_type == '2':
            cur_has_jump = ""
            cur_jump_to = ""
            if has_jump and any_jump and str(any_jump) != '0':
                cur_has_jump = has_jump
                cur_jump_to = any_jump
            title_is_text = True
            sys_type = SysTypeEnum.TEXT.value
            option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type,
                                   is_jump=cur_has_jump, jump_to=cur_jump_to, req=cur_req, is_text=title_is_text,
                                   relation=relation, data_index=cur_index)
            options.append(option_obj.__dict__)
            di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type,
                           req=cur_req, is_text=title_is_text, is_jump=cur_has_jump, jump_to=cur_jump_to,
                           relation=relation, options=options)
            data_index.append(di.__dict__)
            item_data_index.append(cur_index)
            cur_index += 1
        elif cur_type == '3':
            # 情景题处理（即随机题）
            if '1' == question.attrs.get('qingjing'):
                raise Exception('第' + title_id + '题为情景题无法处理')
            radios = question.select('.ui-radio')
            # 获取合法的值
            valid_values = [-3]
            # 生成对应的对象
            for j in radios:
                option_key = j.select_one('.label').text
                is_text = 1 if j.select_one('.ui-text') else 0
                cur_span = j.select('.jqradiowrapper')
                option_value = ''
                cur_jump = any_jump
                cur_has_jump = has_jump
                for k in cur_span:
                    cur_option = k.find('input')
                    option_value = cur_option.attrs.get('value')
                    valid_values.append(option_value)
                    # 如果外部指定了整个题目的跳转逻辑，则直接跳转整个题目，否则不跳转
                    if has_jump and any_jump and str(any_jump) != '0':
                        pass
                    else:
                        cur_jump = cur_option.attrs.get('jumpto')
                        cur_has_jump = 1 if cur_jump else 0
                option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type,
                                       name=option_key, value=option_value, is_jump=cur_has_jump, jump_to=cur_jump,
                                       req=cur_req, is_text=is_text, relation=relation, data_index=cur_index)
                options.append(option_obj.__dict__)
            di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type,
                           name=title, is_jump=has_jump, jump_to=any_jump, req=cur_req, relation=relation,
                           options=options, valid_values=valid_values)
            data_index.append(di.__dict__)
            item_data_index.append(cur_index)
            cur_index += 1
        elif cur_type == '4':
            cur_min_oper_num = 1 if cur_req and str(cur_req) == '1' else 0
            radios = question.select('.ui-checkbox')
            cur_max_oper_num = cur_max_oper_num or len(radios)
            valid_values = [-3, -2, 0, 1]  # 合法的参数
            for j in radios:
                option_key = j.select_one('.label').text
                is_text = True if j.select_one('.ui-text') else False
                cur_span = j.select('.jqcheckwrapper')
                option_value = ''
                for k in cur_span:
                    cur_option = k.find('input')
                    option_value = cur_option.attrs.get('value')
                name = option_key
                multiple_title = title + name
                option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=multiple_title, title_type=cur_type, sys_type=sys_type,
                                       name=name, value=option_value, is_jump=has_jump, jump_to=any_jump, req=cur_req,
                                       is_text=is_text, relation=relation, data_index=cur_index)
                option_obj_dict = option_obj.__dict__
                options.append(option_obj_dict)
                di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=multiple_title, title_type=cur_type,
                               sys_type=sys_type, name=name, value=option_value, is_jump=has_jump, jump_to=any_jump,
                               req=cur_req, is_text=is_text, relation=relation, options=[option_obj_dict],
                               valid_values=valid_values)
                data_index.append(di.__dict__)
                item_data_index.append(cur_index)
                cur_index += 1
        elif cur_type == '5':
            valid_values = [-3]  # 合法的参数
            selects = question.select_one('.scale-rating')
            select_options = selects.select('li')
            for j in select_options:
                cur_a = j.select_one('a')
                a_value = cur_a.attrs.get('val')
                a_title = cur_a.attrs.get('title')
                valid_values.append(a_value)
                option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type,
                                       name=a_title, value=a_value, is_jump=has_jump, jump_to=any_jump, req=cur_req,
                                       relation=relation, data_index=cur_index)
                options.append(option_obj.__dict__)
            di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type,
                           is_jump=has_jump, jump_to=any_jump, req=cur_req, relation=relation, options=options,
                           valid_values=valid_values)
            data_index.append(di.__dict__)
            item_data_index.append(cur_index)
            cur_index += 1
        elif cur_type == '6':
            is_check = question.attrs.get('ischeck')  # 用来判断题目的类型
            if is_check and "1" == str(is_check):  # 矩阵多选
                matrix_multiple_min = 1 if cur_req == '1' else 0  # 最小的值
                sys_type = SysTypeEnum.MULTIPLE_MATRIX.value  # 设置类型为矩阵多选
                cur_tbody = question.select_one('tbody')
                trs = cur_tbody.select('tr')
                if not trs or len(trs) <= 0:
                    break
                valid_values = [-3, -2, 0, 1]  # 合法的参数
                col_options = []  # 每一列的标题
                matrix_multiple_max = len(col_options)  # 最大的值
                sub_title = ""  # 每个子题目的标题
                sub_title_index = 0
                for j in range(len(trs)):
                    tr_tag = trs[j]  # 当前的tr对象
                    if 'class' in tr_tag.attrs and 'trlabel' in tr_tag['class']:  # 当前矩阵量表的选项名称
                        sub_title_index = 0
                        col_options = []
                        # ths = tr_tag.find_all('th')
                        ths = tr_tag.find_all('th', {'class': False})  # 所有的标题
                        for th in ths:
                            if th.string is not None and th.string.strip() != '':  # 判断标签内容是否为空
                                col_options.append(th.string)
                        matrix_multiple_max = len(col_options)
                    elif 'class' in tr_tag.attrs and 'rowtitle' in tr_tag['class']:  # 当前矩阵量表的子标题
                        first_td = tr_tag.find('td')
                        if first_td:
                            sub_title_index += 1
                            sub_title = str(sub_title_index) + '.' + first_td.text
                    elif 'tp' in tr_tag.attrs and 'd' in tr_tag['tp']:  # 说明这个是当前矩阵量表的一个题
                        inner_options = []
                        a_list = tr_tag.select('a')  # 所有选项
                        children_title = title + sub_title
                        for a_index in range(len(a_list)):
                            cur_a = a_list[a_index]
                            cur_value = cur_a.attrs.get('dval')
                            children_option_title = children_title + '.' + col_options[a_index]
                            option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=children_option_title, title_type=cur_type,
                                                   sys_type=sys_type, name=col_options[a_index], value=cur_value,
                                                   is_jump=has_jump, jump_to=any_jump, req=cur_req, relation=relation,
                                                   data_index=cur_index)
                            inner_options.append(option_obj.__dict__)
                            di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=children_option_title,
                                           title_type=cur_type, sys_type=sys_type, name=col_options[a_index],
                                           is_jump=has_jump, jump_to=any_jump, req=cur_req, relation=relation,
                                           options=inner_options, valid_values=valid_values)
                            data_index.append(di.__dict__)
                            item_data_index.append(cur_index)
                            cur_index += 1
                        child_obj = ItemWjxOb(tid=counter, title_id=title_id, title=children_title, title_type=cur_type,
                                              sys_type=sys_type, name=sub_title, is_jump=has_jump, jump_to=any_jump,
                                              req=cur_req, relation=relation, options=inner_options,
                                              data_index=cur_index, min_oper_num=matrix_multiple_min,
                                              max_oper_num=matrix_multiple_max)
                        children.append(child_obj.__dict__)
            else:  # 普通矩阵
                item_questions = question.select_one('.tableWrap')
                cur_tbody = item_questions.select_one('tbody')
                trs = cur_tbody.select('tr')
                if not trs or len(trs) <= 0:
                    break
                # 新写法
                # 1.外部过滤标题
                col_options = []  # 每一列的标题
                sub_title = ""  # 每个子题目的标题
                # 2.内部过滤每个选项
                for j in range(len(trs)):
                    valid_values = [-3]  # 合法的参数
                    tr_tag = trs[j]  # 当前的tr对象
                    if 'class' in tr_tag.attrs and 'trlabel' in tr_tag['class']:  # 当前矩阵量表的选项名称
                        col_options = []
                        # ths = tr_tag.find_all('th')
                        ths = tr_tag.find_all('th', {'class': False})  # 所有的标题
                        for th in ths:
                            if th.string is not None and th.string.strip() != '':  # 判断标签内容是否为空
                                col_options.append(th.string)
                    elif 'class' in tr_tag.attrs and 'rowtitle' in tr_tag['class']:  # 当前矩阵量表的子标题
                        first_td = tr_tag.find('td')
                        if first_td:
                            sub_title = first_td.text
                    elif 'tp' in tr_tag.attrs and 'd' in tr_tag['tp']:  # 说明这个是当前矩阵量表的一个题
                        inner_options = []
                        a_list = tr_tag.select('a')  # 所有选项
                        rowindex = tr_tag.attrs.get('rowindex')
                        sub_title = str(rowindex) + '.' + sub_title
                        dval_list = []  # 所有答案的list
                        for a_index in range(len(a_list)):
                            cur_a = a_list[a_index]
                            cur_value = cur_a.attrs.get('dval')
                            if cur_value:
                                valid_values.append(cur_value)
                                dval_list.append(cur_value)
                        children_title = title + sub_title
                        for a_index in range(len(dval_list)):
                            # cur_a = a_list[a_index]
                            # cur_value = cur_a.attrs.get('dval')
                            # valid_values.append(cur_value)
                            cur_value = dval_list[a_index]
                            name = col_options[a_index]
                            child_option_title = children_title + str(cur_value) + '.' + name
                            option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=child_option_title, title_type=cur_type,
                                                   sys_type=sys_type, name=name, value=cur_value, is_jump=has_jump,
                                                   jump_to=any_jump, req=cur_req, relation=relation,
                                                   data_index=cur_index)
                            inner_options.append(option_obj.__dict__)
                        child_obj = ItemWjxOb(tid=counter, title_id=title_id, title=children_title, title_type=cur_type,
                                              sys_type=sys_type, name=sub_title, is_jump=has_jump, jump_to=any_jump,
                                              req=cur_req, relation=relation, options=inner_options,
                                              data_index=cur_index)
                        children.append(child_obj.__dict__)
                        di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=children_title, title_type=cur_type,
                                       sys_type=sys_type, name=sub_title, is_jump=has_jump, jump_to=any_jump,
                                       req=cur_req, relation=relation, options=inner_options, valid_values=valid_values)
                        data_index.append(di.__dict__)
                        item_data_index.append(cur_index)
                        cur_index += 1
        elif cur_type == '7':
            valid_values = [-3]  # 合法的参数
            selects = question.select_one('.ui-select')
            select_options = selects.select('option')
            for j in select_options:
                cur_value = j.attrs.get('value')
                valid_values.append(cur_value)
                name = j.text
                option_title = title + j.text
                option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=option_title, title_type=cur_type, sys_type=sys_type,
                                       name=name, value=cur_value, is_jump=has_jump, jump_to=any_jump, req=cur_req,
                                       relation=relation, data_index=cur_index)
                options.append(option_obj.__dict__)
            di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type,
                           is_jump=has_jump, jump_to=any_jump, req=cur_req, relation=relation, options=options,
                           valid_values=valid_values)
            data_index.append(di.__dict__)
            item_data_index.append(cur_index)
            cur_index += 1
        elif cur_type == '8':
            valid_values = [-3]  # 合法的参数
            matrix_rating = question.select_one('.ui-slider-input')
            cur_min = int(matrix_rating.attrs.get('min'))
            cur_max = int(matrix_rating.attrs.get('max'))
            for valid_value in range(cur_min, cur_max + 1):
                valid_values.append(valid_value)
            inner_options = ItemWjxOb(tid=counter, title_id=title_id, title=title, title_type=cur_type,
                                      sys_type=sys_type, name=matrix_rating.text, is_jump=has_jump, jump_to=any_jump,
                                      req=cur_req, relation=relation, min_oper_num=cur_min,
                                      max_oper_num=cur_max, data_index=cur_index)
            inner_options_dict = inner_options.__dict__
            inner_child = ItemWjxOb(tid=counter, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type,
                                    name=matrix_rating.text, is_jump=has_jump, jump_to=any_jump, req=cur_req,
                                    relation=relation, options=[inner_options_dict], min_oper_num=cur_min,
                                    max_oper_num=cur_max, data_index=cur_index)
            children.append(inner_child.__dict__)
            di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=title, title_type=cur_type,
                           sys_type=sys_type, name=matrix_rating.text, is_jump=has_jump, jump_to=any_jump, req=cur_req,
                           relation=relation, options=[inner_options_dict], min_oper_num=cur_min,
                           max_oper_num=cur_max, valid_values=valid_values)
            data_index.append(di.__dict__)
            item_data_index.append(cur_index)
            cur_index += 1
        elif cur_type == '9':
            gapfill = question.attrs.get('gapfill')  # 用来判断题目的类型
            if gapfill and "1" == str(gapfill):  # 多项填空
                sys_type = SysTypeEnum.MULTIPLE_TEXT.value  # 设置类型为多项填空
                # 找到指定的标签
                target_tag = question.find('div', class_='topichtml')
                # 遍历标签及其子元素
                text_name = ""
                answer_9_index = 0  # 每有1个空，答案序号+1
                for tag in target_tag.descendants:
                    if tag.string:  # 这个是纯文本内容
                        text_name = tag.string.strip()
                    if tag.name and 'input' == tag.name:  # 如果标签，且是一个input标签，则增加一个子选项
                        answer_9_index += 1
                        cur_name = str(answer_9_index) + '.' + text_name
                        cur_title = title + cur_name
                        inner_options = ItemWjxOb(tid=counter, title_id=title_id, title=cur_title, title_type=cur_type, is_text=True,
                                                  sys_type=sys_type, name=cur_name, is_jump=has_jump, jump_to=any_jump,
                                                  req=cur_req, relation=relation, data_index=cur_index)
                        inner_options_dict = inner_options.__dict__
                        inner_child = ItemWjxOb(tid=counter, title_id=title_id, title=cur_title, title_type=cur_type,
                                                sys_type=sys_type, name=cur_name, is_jump=has_jump, jump_to=any_jump,
                                                req=cur_req, is_text=True, relation=relation,
                                                options=[inner_options_dict], data_index=cur_index)
                        children.append(inner_child.__dict__)
                        di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=cur_title, title_type=cur_type,
                                       sys_type=sys_type, is_jump=has_jump, jump_to=any_jump, req=cur_req, is_text=True,
                                       relation=relation, options=[inner_options_dict])
                        data_index.append(di.__dict__)
                        item_data_index.append(cur_index)
                        cur_index += 1
            else:
                # 滑杆题 / 矩阵填空
                valid_values = list(range(101))  # 合法的参数
                valid_values.append(-3)
                matrix_rating = question.select_one('.matrix-rating')
                trs = matrix_rating.select('tr')
                for j in range(len(trs)):
                    if j % 2 == 1:
                        continue
                    o = trs[j]
                    cur_text_id = o.attrs.get('id')
                    values = matrix_rating.select_one('#' + cur_text_id[:-1])
                    cur_slider = values.select_one('.ui-slider-input')
                    cur_text = values.select_one('.ui-input-text')
                    cur_title = title + o.text
                    if cur_slider:
                        sys_type = SysTypeEnum.SLIDE.value  # 设置类型为多项滑杆题
                        cur_min = cur_slider['min']
                        cur_max = cur_slider['max']
                        inner_options = ItemWjxOb(tid=counter, title_id=title_id, title=cur_title, title_type=cur_type,
                                                  sys_type=sys_type, name=o.text, is_jump=has_jump, jump_to=any_jump,
                                                  req=cur_req, relation=relation, min_oper_num=cur_min,
                                                  max_oper_num=cur_max, data_index=cur_index)
                        inner_options_dict = inner_options.__dict__
                        inner_child = ItemWjxOb(tid=counter, title_id=title_id, title=cur_title, title_type=cur_type,
                                                sys_type=sys_type, name=o.text, is_jump=has_jump, jump_to=any_jump,
                                                req=cur_req, relation=relation, options=[inner_options_dict],
                                                min_oper_num=cur_min, max_oper_num=cur_max, data_index=cur_index)
                        children.append(inner_child.__dict__)
                        di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=cur_title, title_type=cur_type,
                                       sys_type=sys_type, name=o.text, is_jump=has_jump, jump_to=any_jump, req=cur_req,
                                       relation=relation, options=[inner_options_dict], min_oper_num=cur_min,
                                       max_oper_num=cur_max, valid_values=valid_values)
                        data_index.append(di.__dict__)
                        item_data_index.append(cur_index)
                        cur_index += 1
                    if cur_text:
                        sys_type = SysTypeEnum.MATRIX_TEXT.value  # 设置类型为矩阵填空
                        inner_options = ItemWjxOb(tid=counter, title_id=title_id, title=cur_title, title_type=cur_type,
                                                  sys_type=sys_type, name=o.text, is_jump=has_jump, jump_to=any_jump,
                                                  req=cur_req, relation=relation, data_index=cur_index, is_text=True)
                        inner_options_dict = inner_options.__dict__
                        inner_child = ItemWjxOb(tid=counter, title_id=title_id, title=cur_title, title_type=cur_type,
                                                sys_type=sys_type, name=o.text, is_jump=has_jump, jump_to=any_jump,
                                                req=cur_req, relation=relation, options=[inner_options_dict],
                                                data_index=cur_index, is_text=True)
                        children.append(inner_child.__dict__)
                        di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=cur_title, title_type=cur_type,
                                       sys_type=sys_type, is_jump=has_jump, jump_to=any_jump, req=cur_req, is_text=True,
                                       relation=relation, options=[inner_options_dict], valid_values=valid_values)
                        data_index.append(di.__dict__)
                        item_data_index.append(cur_index)
                        cur_index += 1
        elif cur_type == '10':
            pass
        elif cur_type == '11':
            cur_min_oper_num = 1 if cur_req and str(cur_req) == '1' else 0
            radios = question.select('.ui-li-static')
            cur_max_oper_num = cur_max_oper_num or len(radios)
            # 生成所有合法参数
            valid_values = [-3, -2]
            for j in radios:
                cur_option = j.find('input')
                option_value = cur_option.attrs.get('value')
                valid_values.append(option_value)
            # 解析所有题目
            for j in radios:
                option_key = j.text
                is_text = True if j.select_one('.ui-text') else False
                cur_option = j.find('input')
                option_value = cur_option.attrs.get('value')
                cur_jump = cur_option.attrs.get('jumpto')
                option_title = title + option_key
                option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=option_title, title_type=cur_type, sys_type=sys_type,
                                       name=option_key, value=option_value, is_jump="1" if cur_jump else "",
                                       jump_to=cur_jump, req=cur_req, is_text=is_text, relation=relation, options=None,
                                       children=None, min_oper_num=None, max_oper_num=None, data_index=cur_index)
                option_obj_dict = option_obj.__dict__
                options.append(option_obj_dict)
                di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=option_title, title_type=cur_type,
                               sys_type=sys_type, name=option_key, value=option_value, is_jump="1" if cur_jump else "",
                               jump_to=cur_jump, req=cur_req, is_text=is_text, relation=relation,
                               options=[option_obj_dict], children=None, min_oper_num=None, max_oper_num=None,
                               valid_values=valid_values)
                data_index.append(di.__dict__)
                item_data_index.append(cur_index)
                cur_index += 1
        elif cur_type == '12':
            valid_values = list(range(101))  # 合法的参数
            valid_values.append(-3)
            matrix_rating = question.select_one('.matrix-rating')
            total = question.attrs.get('total')
            trs = matrix_rating.select('tr')
            for j in range(len(trs)):
                cur_has_jump = ""
                cur_jump_to = ""
                if has_jump and any_jump and str(any_jump) != '0':
                    cur_has_jump = has_jump
                    cur_jump_to = any_jump
                if j % 2 == 1:
                    continue
                o = trs[j]
                cur_text_id = o.attrs.get('id')
                values = matrix_rating.select_one('#' + cur_text_id[:-1])
                cur_option = values.select_one('.ui-slider-input')
                option_title = title + o.text
                option_obj = ItemWjxOb(tid=counter, title_id=title_id, title=option_title, title_type=cur_type, sys_type=sys_type,
                                       name=o.text, req=cur_req, relation=relation, is_jump=cur_has_jump,
                                       min_oper_num=cur_option.attrs.get('min'), jump_to=cur_jump_to,
                                       max_oper_num=cur_option.attrs.get('max'), data_index=cur_index)
                options.append(option_obj.__dict__)
                di = DataIndex(tid=counter, index=cur_index, title_id=title_id, title=option_title, title_type=cur_type,
                               sys_type=sys_type, name=o.text, req=cur_req, relation=relation,
                               min_oper_num=cur_option.attrs.get('min'), max_oper_num=cur_option.attrs.get('max'),
                               valid_values=valid_values, jump_to=cur_jump_to, is_jump=cur_has_jump)
                data_index.append(di.__dict__)
                item_data_index.append(cur_index)
                cur_index += 1

        item_obj = ItemWjxOb(tid=counter, title_id=title_id, title=title, title_type=cur_type, sys_type=sys_type, name=title,
                             is_jump=has_jump, jump_to=any_jump, req=cur_req, is_text=title_is_text, relation=relation,
                             options=options, children=children, min_oper_num=cur_min_oper_num,
                             max_oper_num=cur_max_oper_num, data_index=item_data_index)
        counter += 1
        res_list.append(item_obj.__dict__)
    return res_list, data_index


if __name__ == '__main__':
    # explain_www_wjx_cn('https://www.wjx.cn/vm/reLD2t6.aspx') # 简单
    # final_res = explain_www_wjx_cn('https://www.wjx.cn/vm/OtcBz7v.aspx')  # 中等
    final_res, data_index = explain_www_wjx_cn('https://www.wjx.cn/vm/OoVY3JO.aspx')  # 难
    print()
    print(json.dumps(final_res))
    print(json.dumps(data_index))
    exit()
